//
//  TodaySwfitTabBarViewController.swift
//  todaySwift
//
//  Created by ChokWah on 2018/3/5.
//  Copyright © 2018年 ChokWah. All rights reserved.
//

import UIKit

class TodaySwfitTabBarViewController: UITabBarController, TS_TabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        addAllChildViewControllers()
        let addTabbar = TS_addTabBar()
        addTabbar.tdelegate = self
        addTabbar.shadowImage = UIImage()
        self.setValue(addTabbar, forKey: "tabBar")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        for subTabbar in tabBar.subviews {
            if subTabbar.isKind(of: NSClassFromString("UITabBarButton")!)  {
                subTabbar.width = tabBar.width / CGFloat(tabBar.subviews.count)
            }
        }
    }
    
    func tabBarDidClickPlusButton(tabBar: TS_addTabBar) {
        print("中间按钮点击")
    }
    
    func addAllChildViewControllers() {
        // 这里建立4个bar，如果是中间突出的需要自己定义一个uitabbar，重写位置
        let homeVC = TS_HomeViewController()
        self.createChildVCWithVC(childVC: homeVC, title: "首页", image: "home_tabbar_32x32_", selectImage:"home_tabbar_press_32x32_")
        
        let videoVC = TS_VideoViewController()
        self.createChildVCWithVC(childVC: videoVC, title: "视频", image: "video_tabbar_32x32_", selectImage:"video_tabbar_press_32x32_")
        
        let huoshanVC = TS_HuoShanViewController()
        self.createChildVCWithVC(childVC: huoshanVC, title: "小视频", image: "huoshan_tabbar_32x32_", selectImage:"huoshan_tabbar_press_32x32_")
        
        let mineVC = TS_MineViewController()
        self.createChildVCWithVC(childVC: mineVC, title: "我的", image: "mine_tabbar_32x32_", selectImage:"mine_tabbar_press_32x32_")
        
    }

    func createChildVCWithVC(childVC : UIViewController, title: String, image imageName: String, selectImage selectImageName: String) {
        
        childVC.title = title
        
        let textAttrs = [NSAttributedStringKey.foregroundColor : UIColor.black]
        let selectTextAttrs = [NSAttributedStringKey.foregroundColor : UIColor.black]
        childVC.tabBarItem.setTitleTextAttributes(textAttrs, for: .normal)
        childVC.tabBarItem.setTitleTextAttributes(selectTextAttrs, for: .selected)
        childVC.tabBarItem.image = UIImage(named: imageName)
        childVC.tabBarItem.selectedImage = UIImage(named: selectImageName)?.withRenderingMode(.alwaysOriginal)
        
        let nav = TodaySwiftNavigationController(rootViewController: childVC)
        self.addChildViewController(nav)
    }
}

extension UITabBarController {
    class func CreateItemWithTarget(target : Any, ForAction action: Selector,  WithImageName name: String, WithHighlightImageName  hightlightName : String) -> UIBarButtonItem {
        
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: name), for: .normal)
        btn.setBackgroundImage(UIImage(named: hightlightName), for: .highlighted)
        btn.addTarget(target, action: action, for: .touchUpInside)
        btn.size = (btn.currentBackgroundImage?.size)!
        return UIBarButtonItem(customView: btn)
    }
}
